<?php
error_reporting(1);

/* * * * * * * * * * * * * * * *\

     function declarations
     
\* * * * * * * * * * * * * * * */

function write_log($note) {
    $keys = array_keys($_REQUEST);
    $message = 0;
    foreach($keys as $key):
     if ($key != "vector"):
	      $message .= "&$key={$_REQUEST[$key]}";
	  endif;
    endforeach;
	$myFile = "upload_log.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData = "note: $note \n time: " . date(DATE_RFC822) . "\nrequests:$message\n\n";
	fwrite($fh, $stringData);
	fclose($fh);
}	

write_log("trying");

/* * * * * * * * * * * * * * * *\

      get request variables

\* * * * * * * * * * * * * * * */

//This is the unique hash associated with a crop
$action = $_REQUEST['action'];
$savename = $_POST['savename'];
$hashkey = ((isset($_POST['savename']))?$_POST['savename']:"test");
$sessId = $_REQUEST['sessId'];


switch($action)
{	
	case "crop":
	
	//decode string from flash
	$vectorData = base64_decode($_POST["vector"]);

	//create image with transparancey restored
	$vectorLayer = imagecreatefromstring( $vectorData ) or write_log("error=baddata");
    imagealphablending($vectorLayer, false);
	imagesavealpha($vectorLayer, true);
	
	//save width and height
	$width = imagesx($vectorLayer);
	$height = imagesy($vectorLayer);
	
	//create image same size as vector layer
	$backgroundLayer = imagecreatetruecolor($width, $height);
		$transparent = imagecolorallocatealpha($backgroundLayer, 67 , 67 , 67 , 127);
		imagefill($backgroundLayer, 0 , 0 , $transparent);	
     	  	imagealphablending($backgroundLayer, false);
		imagesavealpha($backgroundLayer, true);


		//parse out the data for images
		$image_list = split(",",$_POST['images']);
		foreach($image_list as $img)
		{
			//extract info from this string
			list($src, $imgwidth, $imgheight, $imgx , $imgy ) = split("->", $img);
		
			//rebuild the image here, flipping if necessary
			
			$tempLayer = imagecreatefromstring( file_get_contents($src) );
			$imgrwidth = imagesx($tempLayer); //real width of image
			$imgrheight = imagesy($tempLayer); //real height of image

			//if image is flipped horizontally, then the x is set at the right side of the image
			//and the width is negative
			//
			//if it's flipped vertically, then the y is set at the bottom side of the image
			//and the height is negative
			//
			//both are true if bot are flipped
			//
			//imagecopyresampled() will flip the image internally and expects the real leftbound and upperbound x,y
			//and will start filling in rightward and downward.  That means we need to know beforehand if we need to subtract
			//the scaled size from the x,y to get back to the original, unflipped x,y
			
			$srcx = 0; //where to start copying from on the tempLayer
			$srcy = 0; //where to start copying form on th tempLayer

			if($imgwidth < 0)
			{
				$imgx += $imgwidth; //where to start copying onto the destination
				$imgwidth = -$imgwidth; //we draw it left to right, so make positive
				$srcx = $imgrwidth - 1; //start at the far right of tempLayer
				$imgrwidth = -$imgrwidth; //turn this negative to sample it right to left	
			}
			if($imgheight < 0)
			{
				$imgy += $imgheight; //where to start copying onto the destination
				$imgheight = -$imgheight; //we draw it top to bottom, so make positive
				$srcy = $imgrheight - 1; //start at the far bottom of tempLayer
				$imgrheight = -$imgrheight; //turn this negative to sample it bottom to top
			}

			//now copy it to background..
			imagecopyresampled($backgroundLayer, $tempLayer , $imgx, $imgy, $srcx , $srcy , $imgwidth, $imgheight, $imgrwidth, $imgrheight);
			imagedestroy($tempLayer);
		}


	//add vectorLayer on top
	imagealphablending($vectorLayer,true);
	imagealphablending($backgroundLayer,true);
	imagecopy($backgroundLayer,$vectorLayer, 0,0 , 0, 0, $width, $height);

	//save copy
	//CHANGE NEXT LINE TO SAVE IN UPLOADS AND SAVE TO WHAT NAME YOU WANT
	$file_name = "images/" . $_REQUEST['savename'] . ".png";
	imagepng($backgroundLayer, $file_name) or write_log("error=nosave");

	//tell flash crop is done
	echo "hashkey=" . $savename;
	write_log("success writing file $file_name");
	break;
}



?>